package com.bls.cate.listener;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.EmptyUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.bls.cate.R;
import com.bls.cate.ui.activity.login.LoginActivity;
import com.bls.cate.ui.activity.search_user.SearchUserActivity;
import com.bls.cate.ui.activity.register.RegisterActivity;
import com.bls.cate.ui.activity.write_article.WriteArticleActivity;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/1/15.
 */

public class EditTextChangedListener implements TextWatcher {

    private EditText etPhone, etPwd, etVcode, etTitle;
    private TextView btnRegister, btnLogin, tvTitleNum;
    private Activity mActivity;
    private SearchUserListener searchUserListener;
    private WriteTitleListener writeTitleListener;

    private int maxTitleNum;

    /**
     * 注册
     */
    public EditTextChangedListener(Activity mActivity, EditText etPhone, EditText etPwd, EditText etVcode, TextView btnRegister) {
        this.etPhone = etPhone;
        this.etPwd = etPwd;
        this.etVcode = etVcode;
        this.btnRegister = btnRegister;
        this.mActivity = mActivity;
    }

    /**
     * 登陆
     */
    public EditTextChangedListener(EditText etPhone, EditText etPwd, TextView btnLogin, Activity mActivity) {
        this.etPhone = etPhone;
        this.etPwd = etPwd;
        this.btnLogin = btnLogin;
        this.mActivity = mActivity;
    }

    /**
     * 搜索用户
     */
    public EditTextChangedListener(Activity mActivity, SearchUserListener searchUserListener) {
        this.mActivity = mActivity;
        this.searchUserListener = searchUserListener;
    }


    /**
     * 写文章的标题
     **/
    public EditTextChangedListener(Activity mActivity, EditText etTitle, TextView tvTitleNum, int maxTitleNum, WriteTitleListener listener) {
        this.mActivity = mActivity;
        this.etTitle = etTitle;
        this.tvTitleNum = tvTitleNum;
        this.maxTitleNum = maxTitleNum;
        this.writeTitleListener = listener;
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //注册
        if (mActivity instanceof RegisterActivity) {
            if (RegexUtils.isMobileSimple(etPhone.getText().toString().trim()) && etPwd.getText().toString().trim().length() > 5 && EmptyUtils.isNotEmpty(etVcode.getText().toString().trim()))
                btnRegister.setBackground(mActivity.getDrawable(R.drawable.bg_btn_login_register_1));
            else
                btnRegister.setBackground(mActivity.getDrawable(R.drawable.bg_btn_login_register_2));
        }
        //登陆
        else if (mActivity instanceof LoginActivity) {
            if (RegexUtils.isMobileSimple(etPhone.getText().toString().trim()) && etPwd.getText().toString().trim().length() > 5)
                btnLogin.setBackground(mActivity.getDrawable(R.drawable.bg_btn_login_register_1));
            else btnLogin.setBackground(mActivity.getDrawable(R.drawable.bg_btn_login_register_2));
        }
        //写文章的标题
        else if (mActivity instanceof WriteArticleActivity) {
            int dumpNum = maxTitleNum - s.length();
            tvTitleNum.setText(dumpNum >= 0 ? String.valueOf(dumpNum) : "0");
            etTitle.setSelection(s.length());
            if (dumpNum < 0) {
                String strOver = String.valueOf(s.subSequence(0, maxTitleNum));
                writeTitleListener.OnWriteTitleListener(strOver);
            }
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
        //搜索用户
        if (mActivity instanceof SearchUserActivity) {
            searchUserListener.searchUserResult(s.toString());
        }
    }

    public interface SearchUserListener {
        void searchUserResult(String s);
    }

    public interface WriteTitleListener {
        void OnWriteTitleListener(String s);
    }
}
